package com.zhiyou.app.controller;


import cn.dev33.satoken.annotation.SaIgnore;
import com.aliyuncs.exceptions.ClientException;
import com.zhiyou.app.common.exception.BizException;
import com.zhiyou.app.common.util.SecCheckUtil;
import com.zhiyou.app.common.vo.resp.Response;
import com.zhiyou.app.config.oss.OssUtil;
import com.zhiyou.app.pojo.bo.SecCheckResult;
import com.zhiyou.app.pojo.bo.TaskResult;
import com.zhiyou.app.pojo.vo.TaskResponseVo;
import com.zhiyou.app.service.ITaskService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.List;
import java.util.Objects;
import java.util.UUID;
import java.util.concurrent.ExecutionException;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author jonathan.z
 * @since 2025-07-03
 */
@RestController
@RequestMapping("/task")
@Api(tags = "任务管理")
@Slf4j
public class TaskController {
    @Resource
    private ITaskService taskService;
    @Resource
    private OssUtil ossUtil;
    @Resource
    private SecCheckUtil secCheckUtil;

    @ApiOperation(value = "创建并任务")
    @PostMapping(value = "/doTask/{orderNum}")
    public Response<List<TaskResponseVo>> doTask(@PathVariable("orderNum") String orderNum) throws IOException, ExecutionException, InterruptedException {
        List<TaskResponseVo> task = taskService.createTask(orderNum);
        return Response.successWithData(task);
    }


    @ApiOperation(value = "上传文件")
    @PostMapping("/uploadFile")
    public Response<String> uploadFile(MultipartFile file) throws IOException, ClientException, ExecutionException, InterruptedException {
        String substring = Objects.requireNonNull(file.getOriginalFilename()).substring(file.getOriginalFilename().lastIndexOf("."));
        String url = ossUtil.uploadFile("task-file-" + UUID.randomUUID() + substring, "task", file.getInputStream());
        SecCheckResult secCheckResult = secCheckUtil.waitForSecCheckResult(30, url, null).get();
        if (!secCheckResult.isPassed()) {
            log.info("文件违规：{}，删除文件：{},结果：{}", secCheckResult.getErrMsg(), url, ossUtil.deleteFile(url));
            throw new BizException("图片内容违规");
        } else {
            log.info("头像图片内容合规:{}", url);
        }
        return Response.successWithData(url);
    }

    @ApiOperation(value = "获取任务结果")
    @GetMapping("/getOrderTaskResult")
    public Response<TaskResult> getOrderTaskResult(String orderNum) {
        return Response.successWithData(taskService.getOrderTaskCombineResult(orderNum));
    }

    @SaIgnore
    @ApiOperation(value = "获取任务进度")
    @PostMapping(value = "/getOrderTaskProcess", produces = MediaType.TEXT_EVENT_STREAM_VALUE + ";charset=UTF-8")
    public SseEmitter getTaskProcess(String orderNum) throws IOException {
        return taskService.getTaskProcess(orderNum);
    }
}
